let now = new Date(); //当前日期
let nowDayOfWeek = now.getDay(); //今天本周的第几天
let nowDay = now.getDate(); //当前日
let nowMonth = now.getMonth(); //当前月
let nowYear = now.getYear(); //当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //

let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth()-1);

export const dateInfo = {
  //获取本月列表
  getMonthData(year, month) {
    let ret = [];
    if (!year || !month) {
      let today = new Date();
      year = today.getFullYear();
      month = today.getMonth() + 1;
    }

    let todayDate = new Date().getDate();
    let monthDate = new Date().getMonth() + 1;
    let firstDay = new Date(year, month - 1, 1);
    let firstDayWeekDay = firstDay.getDay();
    if (firstDayWeekDay === 0) {
      firstDayWeekDay = 7;
    }

    let lastDayOfLastMonth = new Date(year, month - 1, 0);
    let lastDateOfLastMonth = lastDayOfLastMonth.getDate();

    let preMonthDayCount = firstDayWeekDay - 1;

    let lastDay = new Date(year, month, 0);
    let lastDate = lastDay.getDate();

    let i;
    let className;
    let isToday = false;
    for (i = 0; i < 7 * 6; i++) {
      let date = i + 1 - preMonthDayCount;
      let showDate = date;
      let thisMonth = month;
      className = [];
      if (date <= 0) {
        thisMonth = month - 1;
        showDate = lastDateOfLastMonth + date;
        className.push("not-thisMonth");
      } else if (date > lastDate) {
        thisMonth = month + 1;
        showDate = showDate - lastDate;
        className.push("not-thisMonth");
      }

      if (thisMonth === 0) {
        thisMonth = 12;
      }
      if (thisMonth === 13) {
        thisMonth = 1;
      }

      isToday = showDate === todayDate && thisMonth === monthDate ? true : false;

      ret.push({
        month: thisMonth,
        date: date,
        showDate: showDate,
        isToday: isToday,
        v: `${year}-${thisMonth}-${showDate}`,
        className: className
      });
    }

    return ret;
  },
}

//获取本周列表
export const _thisWeek = () =>{
  let i,d = [],m = nowMonth + 1;
  let day = nowDay - nowDayOfWeek + 1;
  for (i = 0; i < 7; i++) {
    let v = new Date(nowYear, nowMonth, day + i);
    let isToday = v.getDay() === now.getDay() && v.getMonth() === now.getMonth()?true:false;
    d.push({
      month: m,
      date:"",
      showDate: v.getDate(),
      isToday: isToday,
      v: `${nowYear}-${m}-${v.getDate()}`,
      className: ""
    });
  }
  return d;
}

//格式化日期：yyyy-MM-dd
function formatDate(date) {
  let myyear = date.getFullYear();
  let mymonth = date.getMonth()+1;
  let myweekday = date.getDate();

  if(mymonth < 10){
    mymonth = "0" + mymonth;
  }
  if(myweekday < 10){
    myweekday = "0" + myweekday;
  }
  return (myyear+"-"+mymonth + "-" + myweekday);
}

//获得本周的开始日期
function getWeekStartDate() {
  let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
  return formatDate(weekStartDate);
}

//获得本周的结束日期
function getWeekEndDate() {
  let weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
  return formatDate(weekEndDate);
}

//获得本月的开始日期
function getMonthStartDate(){
  let monthStartDate = new Date(nowYear, nowMonth, 1);
  return formatDate(monthStartDate);
}

//获得本月的结束日期
function getMonthEndDate(){
  let monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
  return formatDate(monthEndDate);
}



